package net.bbtang.website.service.impl;

import java.util.Map;

import net.bbtang.website.mapper.ScoreInfoMapper;
import net.bbtang.website.mapper.ScoreInfoRefMapper;
import net.bbtang.website.pojo.ScoreInfoDTO;
import net.bbtang.website.pojo.ScoreInfoRefDTO;
import net.bbtang.website.service.ScoreInfoService;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;




@Service("scoreInfoService")
public class ScoreInfoServiceImpl implements ScoreInfoService {
	
	private static final Logger logger = Logger.getLogger(ScoreInfoServiceImpl.class);
	
	@Autowired
	ScoreInfoMapper scoreInfoMapper;
	@Autowired
	ScoreInfoRefMapper scoreInfoRefMapper;

	@Override
	public ScoreInfoDTO getScoreInfo(Map<String, Object> map) {
		return scoreInfoMapper.getScoreInfo(map);
	}

	@Override
	public void addScoreInfo(ScoreInfoDTO scoreInfo,
			ScoreInfoRefDTO scoreInfoDetail) {
		int scoreId = scoreInfoMapper.addScoreInfo(scoreInfo);
		scoreInfoDetail.setScoreId(scoreId);
		scoreInfoRefMapper.addScoreInfoDetail(scoreInfoDetail);
	}

	@Override
	public void updateScoreInfo(ScoreInfoDTO scoreInfo,
			ScoreInfoRefDTO scoreInfoDetail) {
		scoreInfoMapper.updateScoreInfo(scoreInfo);
		scoreInfoRefMapper.addScoreInfoDetail(scoreInfoDetail);
		
	}

	
	@Override
	public void changeScoreInfo(Map<String, Object> map) {
		ScoreInfoDTO scoreInfoDTO = getScoreInfo(map);
		ScoreInfoDTO scoreInfo = (ScoreInfoDTO) map.get("scoreInfo");
		ScoreInfoRefDTO scoreInfoDetail = (ScoreInfoRefDTO) map.get("scoreInfoDetail");
		if(scoreInfoDTO != null){
			scoreInfoDetail.setScoreId(scoreInfoDTO.getId());
			scoreInfo.setScore(scoreInfoDTO.getScore()+scoreInfoDetail.getChangeScore());
			updateScoreInfo(scoreInfo,scoreInfoDetail);
		}else{
			scoreInfo.setScore(scoreInfoDetail.getChangeScore());
			addScoreInfo(scoreInfo,scoreInfoDetail);
			
		}
	}
	

	


}
